package com.htgd.gardenexpo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.htgd.gardenexpo.dao.GeConLeisureDataDeckMapper;
import com.htgd.gardenexpo.dao.GeConSportsMaxMapper;
import com.htgd.gardenexpo.dao.WxUserDao;
import com.htgd.gardenexpo.entity.*;
import com.htgd.gardenexpo.dao.GeConLeisureDataDeckTaskMapper;
import com.htgd.gardenexpo.service.IGeConLeisureDataDeckTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htgd.gardenexpo.vo.MileageDurationTrackVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.htgd.gardenexpo.utils.CommonUtils.compareAndReturnLarger;

/**
 * <p>
 * 甲板挑战数据任务 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-04-24
 */
@Service
public class GeConLeisureDataDeckTaskServiceImpl extends ServiceImpl<GeConLeisureDataDeckTaskMapper, GeConLeisureDataDeckTask> implements IGeConLeisureDataDeckTaskService {

    @Autowired
    private GeConLeisureDataDeckTaskMapper geConLeisureDataDeckTaskMapper;

    @Autowired
    private GeConLeisureDataDeckMapper geConLeisureDataDeckMapper;

    @Autowired
    private WxUserDao wxUserDao;

    @Autowired
    private GeConSportsMaxMapper geConSportsMaxMapper;

    @Override
    public CommonResult updateStatus(String taskId) {
        if(taskId==null){
            return CommonResult.error("缺少任务id");
        }
        boolean exists = lambdaQuery().eq(GeConLeisureDataDeckTask::getId, taskId).exists();
        if(!exists){
            return CommonResult.error("任务id不存在");
        }

        GeConLeisureDataDeck endRecord  =geConLeisureDataDeckMapper.getEndRecord(taskId);

        //更新任务表
        lambdaUpdate().eq(GeConLeisureDataDeckTask::getId,taskId)
                .set(GeConLeisureDataDeckTask::getStatus,"2")
                .set(GeConLeisureDataDeckTask::getEndTime,endRecord.getEndTime())
                .update();

        //更新数据表
        geConLeisureDataDeckMapper.update(new GeConLeisureDataDeck(),new LambdaUpdateWrapper<GeConLeisureDataDeck>().eq(GeConLeisureDataDeck::getTaskId,taskId)
                .set(GeConLeisureDataDeck::getStatus,"2"));


        //最大
        GeConLeisureDataDeckTask geConLeisureDataDeckTask = geConLeisureDataDeckTaskMapper.selectById(taskId);
        MileageDurationTrackVO dataByTaskId = geConLeisureDataDeckMapper.getDataByTaskId(taskId);
        String openId = wxUserDao.getOpenId(geConLeisureDataDeckTask.getDeckId());
        if(openId!=null){
            GeConSportsMax geConSportsMax = geConSportsMaxMapper.selectOne(new LambdaQueryWrapper<GeConSportsMax>().eq(GeConSportsMax::getOpenId, openId));
            if(geConSportsMax!=null){
                String maxMileage = compareAndReturnLarger(geConSportsMax.getMaxMileage(), String.valueOf(dataByTaskId.getMileage()));
                String maxDuration = compareAndReturnLarger(geConSportsMax.getMaxDuration(), String.valueOf(dataByTaskId.getDuration()));
                String maxCalorie = compareAndReturnLarger(geConSportsMax.getMaxCalorie(), String.valueOf(dataByTaskId.getCalorie()));
                geConSportsMaxMapper.update(
                        new GeConSportsMax(),  // 实体类实例（通常为空）
                        new LambdaUpdateWrapper<GeConSportsMax>()
                                .eq(GeConSportsMax::getOpenId, openId)  // 设置更新条件：openId 匹配
                                .set(GeConSportsMax::getMaxMileage, maxMileage)  // 设置需要更新的字段值
                                .set(GeConSportsMax::getMaxDuration, maxDuration)  // 更新最大时长
                                .set(GeConSportsMax::getMaxCalorie, maxCalorie)  // 更新最大卡路里
                );
            }else {
                GeConSportsMax geConSportsMax2 = new GeConSportsMax();
                geConSportsMax2.setOpenId(openId);
                geConSportsMax2.setMaxMileage(String.valueOf(dataByTaskId.getMileage()));
                geConSportsMax2.setMaxDuration(String.valueOf(dataByTaskId.getDuration()));
                geConSportsMax2.setMaxCalorie(String.valueOf(dataByTaskId.getCalorie()));
                geConSportsMaxMapper.insert(geConSportsMax2);
            }
        }

        return CommonResult.success("更新成功");
    }

}
